package cn.edu.sgu.www.mhxysy.function.computer.score.impl;

import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuEquipment;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuEquipmentAttribute;
import cn.edu.sgu.www.mhxysy.entity.except.Property;
import cn.edu.sgu.www.mhxysy.enums.PropertyTypes;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.score.ScoreComputer;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuEquipmentAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuEquipmentMapper;
import cn.edu.sgu.www.mhxysy.mapper.except.PropertyMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 宠物装备评分计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class ChongwuEquipmentScoreComputer implements ScoreComputer {

    private final PropertyMapper propertyMapper;
    private final ChongwuEquipmentMapper chongwuEquipmentMapper;
    private final ChongwuEquipmentAttributeMapper chongwuEquipmentAttributeMapper;

    @Autowired
    public ChongwuEquipmentScoreComputer(
            PropertyMapper propertyMapper,
            ChongwuEquipmentMapper chongwuEquipmentMapper,
            ChongwuEquipmentAttributeMapper chongwuEquipmentAttributeMapper) {
        this.propertyMapper = propertyMapper;
        this.chongwuEquipmentMapper = chongwuEquipmentMapper;
        this.chongwuEquipmentAttributeMapper = chongwuEquipmentAttributeMapper;
    }

    /**
     * 计算宠物装备评分
     * @param equipmentId 宠物装备ID
     * @return Integer 宠物装备评分
     */
    @Override
    public Integer apply(String equipmentId) {
        ChongwuEquipment chongwuEquipment = chongwuEquipmentMapper.selectById(equipmentId);

        if (chongwuEquipment == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，宠物装备不存在！");
        }

        /*
         * 宠物装备评分 = 白字分 + 绿字分 + 蓝字分
         * 白字分 = 物理伤害 * 1 = 法术伤害 * 1 = 速度 * 1 = 气血 * 0.24 = 物理防御 / 1.5 = 法术防御 / 1.5
         * 绿字分 = 绿字总和 * 2
         * 蓝字分 = 蓝色词条数 * 30（套装和外观各加30评分）
         */
        AtomicInteger score = new AtomicInteger();

        // 计算白字和绿字属性评分
        QueryWrapper<ChongwuEquipmentAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("equipment_id", equipmentId);

        List<ChongwuEquipmentAttribute> list = chongwuEquipmentAttributeMapper.selectList(queryWrapper);

        if (!list.isEmpty()) {
            for (ChongwuEquipmentAttribute chongwuEquipmentAttribute : list) {
                Integer propertyId = chongwuEquipmentAttribute.getPropertyId();
                Property property = propertyMapper.selectById(propertyId);

                if (PropertyTypes.PTSX.getValue().equals(property.getType())) { // 白字属性（普通属性）
                    switch (property.getName()) {
                        case "物理伤害":
                        case "法术伤害":
                        case "速度":
                            // 物理伤害 * 1 = 法术伤害 * 1 = 速度 * 1
                            score.addAndGet(chongwuEquipmentAttribute.getPropertyValue());
                            break;
                        case "气血":
                            // 气血值 * 0.24
                            BigDecimal bigDecimal = BigDecimal.valueOf(chongwuEquipmentAttribute.getPropertyValue());

                            bigDecimal = bigDecimal.multiply(BigDecimal.valueOf(0.24));

                            score.addAndGet(bigDecimal.intValue());
                            break;
                        case "物理防御":
                        case "法术防御":
                            // 物理防御 / 1.5 = 法术防御 / 1.5
                            BigDecimal value = BigDecimal.valueOf(chongwuEquipmentAttribute.getPropertyValue());

                            value = value.divide(BigDecimal.valueOf(1.5), BigDecimal.ROUND_DOWN);

                            score.addAndGet(value.intValue());
                            break;
                        default:
                            break;
                    }
                } else if (PropertyTypes.FJSX.getValue().equals(property.getType())) { // 绿字属性（附加属性）
                    score.addAndGet(chongwuEquipmentAttribute.getPropertyValue() * 2);
                }
            }
        }

        // 计算篮字评分
        if (chongwuEquipment.getTaozhuangId() > 0) {
            score.addAndGet(30);
        }
        if (chongwuEquipment.getWaiguanId() > 0) {
            score.addAndGet(30);
        }

        return score.get();
    }

}